package data.event;

import java.util.ArrayList;
import java.util.Scanner;

public class Event implements Comparable<Event>
{
	private String name;
	public String name() { return name; }
	public void setName(String name) { this.name = name; }
	
	private String description;
	public String description() { return description; }
	public void setDescription(String description) { this.description = description; }
	
	private String location;
	public String location() { return location; }
	public void setLocation(String location) { this.location = location; }
	
	private String date;
	public String date() { return date; }
	public void setDate(String date) { this.date = date; }
	
	private int startTime;
	public int startTime() { return startTime; }
	public void setStartTime(int time) { this.startTime = time; }
	public String startTimeText() { return timeToText(startTime); }
	
	private int endTime;
	public int endTime() { return endTime; }
	public void setEndTime(int time) { this.endTime = time; }
	public String endTimeText() { return timeToText(endTime); }
	
	private String timeToText(int time)
	{
		int hour = (time / 100);
		int minutes = (time % 100);
		String affix = " AM";
		if(hour > 12)
		{
			affix = " PM";
			hour -= 12;
		}
		if(minutes < 10)
			return hour + ":0" + minutes + affix;
		else
			return hour + ":" + minutes + affix;
	}
	
	private ArrayList<String> attendees;
	public ArrayList<String> attendees() { return attendees; }
	
	public Event(String name, String description, String location, String date, int start, int end, 
			String... attendees)
	{
		this.name = name;
		this.location = location;
		this.description = description;
		this.date = date;
		this.startTime = start;
		this.endTime = end;
		this.attendees = new ArrayList<String>(attendees.length + 1);
		for(int i = 0; i < attendees.length; i++)
			this.attendees.add(attendees[i]);
	}
	
	public Event(String data)
	{
		setFromData(data);
	}
	
	public void setFromData(String data)
	{
		Scanner dataScanner = new Scanner(data);
		dataScanner.useDelimiter("#");
		name = dataScanner.next();
		description = dataScanner.next();
		location = dataScanner.next();
		date = dataScanner.next();
		startTime = dataScanner.nextInt();
		endTime = dataScanner.nextInt();
		attendees = attendeesFromData(dataScanner.next());
	}
	
	public boolean conflictsWith(Event event)
	{
		return event.startTime() < endTime && event.endTime > startTime;
	}
	
	public String toData()
	{
		return  name + "#" +
				description + "#" +
				location + "#" +
				date + "#" + 
				startTime + "#" +
				endTime + "#" +
				attendeesToData();
	}
	
	private ArrayList<String> attendeesFromData(String data)
	{
		ArrayList<String> attendees = new ArrayList<String>();
		if(!data.equals("="))
		{
			Scanner dataScanner = new Scanner(data);
			dataScanner.useDelimiter("%");
			while(dataScanner.hasNext())
				attendees.add(dataScanner.next());
		}
		return attendees;
	}
	
	private String attendeesToData()
	{
		if(attendees.size() == 0)
			return "=";
		String data = "";
		for(int i = 0; i < attendees.size(); i++)
		{
			if(i > 0)
				data += "%";
			data += attendees.get(i);
		}
		return data;
	}

	@Override public int compareTo(Event e)
	{
		int result = startTime - e.startTime;
		if(result == 0)
		{
			result = endTime - e.endTime;
			if(result == 0)
				return name.compareTo(e.name);
		}
		return result;
	}
	
	public void saveData() { }
}